<?php
	session_start();
	require('Tourny/tourny_functions.php');
	
	dblogin();
	
/***********************  Voting! **********************************/
	if($_POST['vote'] != ''){
		$value = $_POST['vote'];
		$tid = $_POST['tid'];
		
		/* check to see if that player has a vote there, and if so, update else insert*/
		$query = "select g.t_id from TOURNY_VOTE g where g.t_id = '$tid' and g.player = {$_SESSION['USERNUM']}";
		$result = mysql_query($query);
		if($row = mysql_fetch_array($result)){
			/* update */
			$query = "update TOURNY_VOTE g  set vote = '$value' where g.t_id = '$tid' and player = {$_SESSION['USERNUM']};";
			$result = mysql_query($query);
		}else{
			/* insert */
			$query = "insert into TOURNY_VOTE values ($tid, {$_SESSION['USERNUM']},'$value','?');";
			$result = mysql_query($query);
		}
	}

	if($_POST['rsvp'] != ''){
		$rsvp = $_POST['rsvp'];
		$tid = $_POST['tid'];
		
		/* check to see if that player has a vote there, and if so, update else insert*/
		$query = "select g.t_id from TOURNY_VOTE g where g.t_id = '$tid' and g.player = {$_SESSION['USERNUM']}";
		$result = mysql_query($query);
		if($row = mysql_fetch_array($result)){
			/* update */
			$query = "update TOURNY_VOTE g  set rsvp = '$rsvp' where g.t_id = '$tid' and player = {$_SESSION['USERNUM']};";
			$result = mysql_query($query);
		}else{
			/* insert */
			$query = "insert into TOURNY_VOTE values ($tid, {$_SESSION['USERNUM']},'NV','$rsvp');";
			$result = mysql_query($query);
		}
	}

	if($_POST['bring'] != ''){
		 $bring_num = substr($_POST['num'],0,1);
		 $bring_val = $_POST['bring'];
		 $game_no = substr($_POST['num'],1,1);
		$tid = $_POST['tid'];

		/* check to see if that player has a vote there, and if so, update else insert*/
		$query = "select g.t_id, g.bring_val from BRING_VOTE g where g.t_id = '$tid' and g.player = {$_SESSION['USERNUM']} and bring_num = $bring_num and game_no = '$game_no'";
		$result = mysql_query($query);
		if($row = mysql_fetch_array($result)){
			if($bring_val == '+'){
				$me_bring_val = $row['bring_val'] + 1;
				/* update */
				$query = "update BRING_VOTE g  
					 		 set bring_val = '$me_bring_val' 
						   where g.t_id = '$tid' and player = {$_SESSION['USERNUM']} 
						     and bring_num = $bring_num and game_no = '$game_no';";
				$result = mysql_query($query);
			}
			if($bring_val == '-' and $row['bring_val'] > 0){
				$me_bring_val = $row['bring_val'] - 1;
				/* update */
				$query = "update BRING_VOTE g  
					 		 set bring_val = '$me_bring_val' 
						   where g.t_id = '$tid' and player = {$_SESSION['USERNUM']} 
						     and bring_num = $bring_num and game_no = '$game_no';";
				$result = mysql_query($query);
			}
		}else{
			$me_bring_val = 0;
			if($bring_val == '+'){
				$me_bring_val = 1;
			}
			/* insert */
			$query = "insert into BRING_VOTE values ($tid, '$game_no', $bring_num, {$_SESSION['USERNUM']}, $me_bring_val);";
			$result = mysql_query($query);
		}
	}
	


/************************   Display the Information  *************************************/	
	$gameno = $_GET['gameno'];
	if($gameno == ''){
		$gameno = -1;
	}
	if($gameno < 0){
		dblogin();
		$query = 
		'select max(game_no) as "GAMENO" from TOURNY';	
		if($result = mysql_query($query)){
			if($row = mysql_fetch_array($result)){
				$gameno = $row['GAMENO'];
			}
		}
	}
	
	$gamea_id = 0;
	$gameb_id = 0;
	$query2 = 
	'select t_id, game_day, gamea_id, gamea_gp, gameb_id, gameb_gp, post, close_poll_dt, vote from TOURNY t where t.game_no = '.$gameno;	
	if($result2 = mysql_query($query2)){
		if($row2 = mysql_fetch_array($result2)){
			$tid = $row2['t_id'];
		 	$gameday = $row2['game_day'];
			$gamea_id = $row2['gamea_id'];
			$gamea_gp = $row2['gamea_gp'];
			$gameb_id = $row2['gameb_id'];
			$gameb_gp = $row2['gameb_gp'];
			$post = $row2['post'];
			$close_poll_dt = $row2['close_poll_dt'];
			$vote = $row2['vote'];
		}
	}

	$query3 =
	'select sum(if(t.rsvp = \'Y\',1,0)) as "NUM_POS",
			sum(if(t.rsvp = \'N\',1,0)) as "NUM_NEG",
			sum(if(t.rsvp = \'?\',1,0)) as "NUM_IDK"
	  from TOURNY_VOTE t
	  where t.t_id = '.$tid;
	 if($result3 = mysql_query($query3)){
		if($row3 = mysql_fetch_array($result3)){
			$num_pos = $row3['NUM_POS'];
			$num_neg = $row3['NUM_NEG'];
			$num_idk = $row3['NUM_IDK'];
		}
	}

	$num_pos = $num_pos *15;
	$num_neg = $num_neg *15;
	$num_idk = $num_idk *15;

?>

<script type="text/javascript">
$(document).ready(function(){
	/* Ajax voting*/

	$(".vote").click(function(){
		$("#ajax_tourny").load("tourny.php",{vote: $(this).attr("name"), tid:'<?php echo $tid; ?>'});
   	});

	$(".rsvp").click(function(){
		$("#ajax_tourny").load("tourny.php",{rsvp: $(this).attr("name"), tid:'<?php echo $tid; ?>'});
   	}); 
	$(".bring").click(function(){
		$("#ajax_tourny").load("tourny.php",{num: $(this).attr("name"), bring:$(this).attr("value"),  tid:'<?php echo $tid; ?>'});
   	});
	
	$('.hover').cluetip({
		positionBy: 'bottomTop', 
		topOffset: 10,
		tracking: false,
		attribute: 'dir',
		showTitle: false,
		cluetipClass: 'jtip',
		sticky: false,
		dropShadow: false
	});
	
	$('.rsvphover').cluetip({
		tracking: true,
		attribute: 'dir',
		showTitle: false,
		cluetipClass: 'jtip',
		sticky: false,
		dropShadow: false
	});

	
	$('.localhover').cluetip({
		tracking: true,
		splitTitle: '|',
		showTitle: false,
		cluetipClass: 'jtip',
		sticky: false,
		dropShadow: false
	});
});
</script>



	<h1> Game # <?php echo $gameno; ?></h1>
	<h2> <?php echo $gameday; ?></h2>					

	<div id="versus">
		<div id="showgame" class="A">
			<?php show_game_tourny($tid, 'A', $gamea_id, $gamea_gp); ?>
		</div>
		<div id="showgame" class="B">
			<?php show_game_tourny($tid, 'B', $gameb_id, $gameb_gp); ?>
		</div>
	</div>
	
<?php 				
	if(loggedin()){
		$query4 =
			'select rsvp
			   from TOURNY_VOTE t
			  where t.t_id = '.$tid.'
			    and t.player = '.$_SESSION['USERNUM'];
		 if($result4 = mysql_query($query4)){
			if($row4 = mysql_fetch_array($result4)){
				$myrsvp = $row4['rsvp'];
			}
		}
?>

	<div id="rsvp_vote">
		<h1>Rsvp: </h1>
		<div id="pos">
			<input type="submit" class="rsvp <?php if($myrsvp == 'Y'){echo 'selected ';} ?>" name="Y" value="+">
			<div id="status" class="rsvphover"	dir="rsvp_players.php?tid=<?php echo $tid; ?>&rsvp=Y" style="height:<?php echo $num_pos; ?>px"></div>
		</div>
		<div id="neg">
			<input type="submit" class="rsvp <?php if($myrsvp == 'N'){echo 'selected ';} ?>" name="N" value="-">
			<div id="status" class="rsvphover"	dir="rsvp_players.php?tid=<?php echo $tid; ?>&rsvp=N" style="height: <?php echo $num_neg; ?>px"></div>
		</div>
		<div id="idk">
			<input type="submit" class="rsvp <?php if($myrsvp == '?'){echo ' selected ';} ?>" name="?" value="?">
			<div id="status" class="rsvphover"	dir="rsvp_players.php?tid=<?php echo $tid; ?>&rsvp=?" style="height: <?php echo $num_idk; ?>px"></div>
		</div>
	</div>

<?php
	}
?>
